document.addEventListener('DOMContentLoaded', function() {
  const inputTextarea = document.getElementById('inputTextarea');
  const outputPre = document.getElementById('outputPre');

  inputTextarea.addEventListener('input', function() {
    const inputText = inputTextarea.value;

	
	const text = inputText
	const regex = /(\S+)\[(\d+)kg\],(\d+),g/g;
	const result = [];
	var l = 20 //间距

	let match;
	while ((match = regex.exec(text)) !== null) {
		let str = match[1] + match[3] + 'g'
		let str_l = match[1].length*2+match[3].length + 1
		if (str_l < l) {
			const spacesToAdd = l - str_l;
			const spaces = ' '.repeat(spacesToAdd);
			str = str +',' + spaces;
		}
		
		
	    result.push(str);
	}
	
	const NewText = result;
	const NewResult = [];
	for (let i = 0; i < NewText.length; i += 4) {
	  const row = NewText.slice(i, i + 4);
	  NewResult.push(row.join(''));
	}
		
	outputPre.textContent = NewResult.join('\n');
    //outputPre.textContent = regexResult;
  });
});
